define(function(){
    var submit = {
        init:function(){
            var thisobj = this;
            thisobj.saveDraft();
            thisobj.preview();
            thisobj.closePrompt();
            var postStatus = 1;
            $('#submit').click(function(event){
                event.preventDefault();
                if(postStatus == 0){
                    return false
                }
                postStatus = 0;
                if(thisobj.checktitle()&&thisobj.checkcoverimg()){
                    var submitUrl = '/article/articleSubmit';
                    var title = $('input[name="title"]').val();
                    var intro = $('#abstract').val();
                    var frontPic = $('#front_pic .cover').find('img').attr('src');
                    var content = UE.getEditor('editor').getContent();
                    var article_id = $("input[name='article_id']").val() || '';
                    var zhongce_id = $("select[name='zhongce_id']").length > 0 ? $("select[name='zhongce_id']").val() : $("input[name='zhongce_id']").val();

                    var tags = [];
                    $(".label li").each(function(){
                        tags.push($(this).attr("data-key"));
                    });
                    $(".errgrid").hide();
                    $.post(submitUrl,{title:title,content:content,img:frontPic,intro:intro,tags:tags,article_id:article_id,zhongce_id:zhongce_id,is_spread:isSpread},function(msg){
                        postStatus = 1;
                        if(msg.status == 200){//清除临时保存数据
                            if(window.localStorage){
                                postStatus = 0;
                                var url = window.location.href;

                                localStorage.removeItem(url);
                                localStorage.removeItem(url+'_date');
                            }
                            location.href = msg.data.url;
                            return false;
                        }else if(msg.status == 510){
                            $("#end_prompt").show()
                        }else{
                            $("#alertbox-content > p").remove();
                            thisobj.showError(msg.msg);
                               $("#alertbox").show();
                        }
                    },'json');

                }else{
                    !thisobj.checkcoverimg() ? $(".formlist dl:eq(3)").find(".errgrid").show() : $(".formlist dl:eq(3)").find(".errgrid").hide();
                    !thisobj.checktitle() ? $(".formlist dl:eq(0)").find(".errgrid").show() : $(".formlist dl:eq(0)").find(".errgrid").hide();
                    !thisobj.checkzhaiyao() ? $(".formlist dl:eq(1)").find(".errgrid").show() : $(".formlist dl:eq(1)").find(".errgrid").hide();
                    !thisobj.checkEditor() ? $(".formlist dl:eq(2)").find(".errgrid").show() : $(".formlist dl:eq(2)").find(".errgrid").hide();
                    //$(".black-bg,.promptinfo").show()
                    postStatus = 1;
                }
                return false
            });
        },
        saveDraft:function(){
            var thisobj = this;

            $('#save').click(function(){
                var submitUrl = '/article/articleSubmit';
                var title = $('input[name="title"]').val();
                var intro = $('#abstract').val();
                var frontPic = $('#front_pic .cover').find('img').attr('src') ? "http://www.shihuo.cn"+$('#front_pic .cover').find('img').attr('src') :'';
                var content = UE.getEditor('editor').getContent();
                var article_id = $("input[name='article_id']").val() || '';
                var zhongce_id = $("select[name='zhongce_id']").length > 0 ? $("select[name='zhongce_id']").val() : $("input[name='zhongce_id']").val();
                var tags = [];

                $(".label li").each(function(){
                    tags.push($(this).attr("data-key"));
                });
                if(tags.length == 0){
                    tags = '';
                }
                $(".errgrid").hide();
                if(title=="" || content==""){
                    $(".alertbox-content > p","#alertbox").remove();
                    submit.showError('请填写标题与内容');
                    $("#alertbox").show();
                    return false;
                }
                $.post(submitUrl,{title:title,content:content,img:frontPic,intro:intro,tags:tags,article_id:article_id,zhongce_id:zhongce_id,is_draft:1},function(msg){
                    if(msg.status == 200){//清除临时保存数据
                        if(window.localStorage){
                            var url = window.location.href;

                            localStorage.removeItem(url);
                            localStorage.removeItem(url+'_date');
                        }

                        location.href = msg.data.url;
                    }else{
                        $(".alertbox-content > p").remove();
                        thisobj.showError(msg.msg);
                        $("#alertbox").show();
                    }
                },'json');
            });
        },
        preview:function(){
            $('#preview').click(function(){
                var title = $('input[name="title"]').val();
                var content = UE.getEditor('editor').getContent();
                var tags = [];

                $(".label li").each(function(){
                    tags.push($(this).attr("data-key"));
                });

                if(!title || !content){
                    $(".alertbox-content > p","#alertbox").remove();
                    submit.showError('请填写标题与内容再预览');
                    $("#alertbox").show();
                    return false;
                }else{
                    $.post('/article/preview',{title:title,content:content,tags:tags},function(msg){
                        if(msg.status){
                            window.open(msg.msg);
                            return true;
                        }else{
                            $(".alertbox-content > p","#alertbox").remove();
                            submit.showError(msg.msg);
                            $("#alertbox").show();
                            return false;
                        }
                    },'json');
                }
            });
        },
        checktitle:function(){
            var hastitle = $("input[name='title']").val() !== "" ? true : false;
            return hastitle
        },
        checkEditor:function(){
            var haseditor = UE.getEditor('editor').getContent() !== "" ? true : false;
            return haseditor
        },
        checkzhaiyao:function(){
            var hasabstract = $("#abstract").val() !== "" ? true : false;
            return hasabstract
        },
        checkcoverimg:function(){
            var hashcover = $(".cover").length > 0 ? true : false;
            return hashcover
        },
        closePrompt:function(){
            $(".alertbox .btn,.alertbox .close,.alertbox-bg").click(function(){
                $(".alertbox").hide();
                $('.alertbox-content p').remove();
            });
        },
        showError:function(msg){
            $("#alertbox-content").prepend("<p>"+msg+"</p>")
        }
    };

    return submit;
});